<template>
  <div>
    <el-dialog
      :title='this.ifAdd+"角色"'
      :visible.sync="addDiavisible"
      width="40%"
      :before-close="cancel"
      @opened="beforeopen"
    >
      <el-form
        :model="form"
        ref="formRef"
        :rules="formRules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAuthPlace, editMechanism,addOrUpdate } from "@/api/family_doctor/family_doctor";
import { inputValidator,nineValidator, selectValidator } from "@/utils/validate";
export default {
  data() {
    return {
      addDiavisible: false,
      data: "",
      placeData: false,
      loading: false,
      ifAdd: "新增",
      form: {
        id: "",
        name: "",
        placeId: "",
        code: "",
        status: "",
      },
      placeId: [],
      temPlaceId:'',
      optionProps: {
        value: "id",
        label: "name",
        children: "childrenNodes",
        checkStrictly: true,
      },
      formRules: {
        name: inputValidator,
        code: nineValidator,
        placeId: selectValidator,
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      for (var key in this.form) {
        this.form[key] = "";
      }
      this.$refs.formRef.clearValidate();
      this.$emit("transData");
      this.addDiavisible = false;
    },
    //弹窗渲染之前
    beforeopen() {},
    //获取地区

    //提交
    addSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return this.$message.error("请输入正确的格式!");
        this.form.placeId = this.temPlaceId
        addOrUpdate(this.form).then((res) => {
          if (res.code == "AA000000") {
            this.$message.success(res.msg);
			 window.location.reload();
          }
          this.cancel();
        });
      });
	 
    },
    //供父组件调用函数
    openDialog(ifAdd, scope) {
      if (ifAdd == true) {
        this.ifAdd = "自定义";
      } else {
        this.ifAdd = "修改";
        this.form.id = scope.id
        this.form.name = scope.name
        this.form.placeId = scope.placeIds.reverse()
        this.temPlaceId = scope.placeId
        this.form.code = scope.code
      }
      this.addDiavisible = true;
    },
  },
};
</script>